using System.IO;
using System.Reflection;
using GrapeCity.Documents.Excel;
using System;

namespace GrapeCity.Documents.Excel.Examples.Templates.DataSource.CustomDataTable
{
    class Program
    {
        static void Main(string[] args)
        {
			//create a new workbook
			var workbook = new GrapeCity.Documents.Excel.Workbook();
			
			// Load template file from resource
			Stream templateFile = GetResourceStream("xlsx\\ComplexMultiDataSource.xlsx");
			workbook.Open(templateFile);
			
			// Create table data sources from JSON
			JsonTable order = new JsonTable(GetResourceStream("json\\order.json"));
			JsonTable customer = new JsonTable(GetResourceStream("json\\customer.json"));
			JsonTable product = new JsonTable(GetResourceStream("json\\product.json"));
			
			// Add data sources
			workbook.AddDataSource("order", order);
			workbook.AddDataSource("customer", customer);
			workbook.AddDataSource("product", product);
			
			// Invoke to process the template
			workbook.ProcessTemplate();
			
			workbook.Worksheets[0].Range["A:F"].ColumnWidth = 16;
			        
			// Save to an excel file
			workbook.Save("CustomDataTable.xlsx");

        }

		static Stream GetResourceStream(string resourcePath)
        {
            string resource = "CustomDataTable.Resource." + resourcePath.Replace("\\", ".");
            var assembly = typeof(Program).GetTypeInfo().Assembly;
            return assembly.GetManifestResourceStream(resource);
        }

    }
}